
from tornado.web import RequestHandler
from utils import photo
from tornado.web import authenticated
from .auth import AuthBaseHandler
from models.account import Post


class IndexHandler(AuthBaseHandler):
    @authenticated
    def get(self):
        images = photo.show_upload()
        self.render("index.html", images=images)


class UploadHandler(AuthBaseHandler):
    """
    图片上传
    """
    def get(self):
        self.render("upload.html")

    def post(self):
        img_file = self.request.files.get("picture")[0]
        save_to = photo.save_upload(img_file)
        path = photo.save_thumbs(save_to, img_file["filename"])
        Post.add_post_for(save_to, path, self.current_user)
        self.write("upload success")
